package com.atguigu.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.common.ShfConstant;
import com.atguigu.entity.UserInfo;
import com.atguigu.entity.vo.LoginVo;
import com.atguigu.entity.vo.RegisterVo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 翟文海
 * @Date: 2022/10/31/031 16:23
 * @Version: 1.0
 * @Description:
 **/
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Reference
    private UserInfoService userInfoService;

    @GetMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable("phone") String phone, HttpSession session) {
        session.setAttribute(ShfConstant.UserConstant.PHONE_CODE, "111");

        return Result.ok();
    }

    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpSession session) {

        //1.判断验证码是否正确
        String sessionCode = (String) session.getAttribute(ShfConstant.UserConstant.PHONE_CODE);
        if (sessionCode == null) {
            Result result = new Result();
            result.setCode(ResultCodeEnum.FAIL.getCode());
            result.setMessage("验证码还未发送");
            return result;

        }
        if (!sessionCode.equalsIgnoreCase(registerVo.getCode())) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //2.判断手机号是否注册过
        UserInfo userInfo = userInfoService.getByPhone(registerVo.getPhone());
        if (userInfo != null) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //3.用户密码加密
        String MD52Password = MD5.encrypt(registerVo.getPassword());
        userInfo = new UserInfo();
        BeanUtils.copyProperties(registerVo, userInfo);
        userInfo.setPassword(MD52Password);
        userInfo.setStatus(1);
        //4.添加数据库注册
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo,HttpSession session) {
        UserInfo userInfo = userInfoService.getByPhone(loginVo.getPhone());
        if (session.getAttribute(ShfConstant.UserConstant.USER_INFO) != null) {
            Result result = new Result();
            result.setCode(ResultCodeEnum.FAIL.getCode());
            result.setMessage("用户已经登录，请勿重复登录！");
            return result;
        }
        //1.判断是否有这个用户
        if (userInfo==null){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        if (userInfo.getStatus() == 0) {
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //3.将密码加密
        String password = MD5.encrypt(loginVo.getPassword());
        //4.判断密码是否正确
        if (!password.equals(userInfo.getPassword())) {
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        //5.登录成功！设置进session域
        session.setAttribute(ShfConstant.UserConstant.USER_INFO, userInfo);

        return Result.ok(userInfo);
    }

    @GetMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute(ShfConstant.UserConstant.USER_INFO);
        return Result.ok();
    }
}
